package io.chenglicun.modules.clc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.entity.BaseEntity;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcQuestionAnswerDao;
import io.chenglicun.modules.clc.dto.ClcQuestionAnswerDTO;
import io.chenglicun.modules.clc.entity.ClcQuestionAnswerEntity;
import io.chenglicun.modules.clc.service.ClcQuestionAnswerService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 题目答案
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-18
 */
@Service
public class ClcQuestionAnswerServiceImpl extends CrudServiceImpl<ClcQuestionAnswerDao, ClcQuestionAnswerEntity, ClcQuestionAnswerDTO> implements ClcQuestionAnswerService {

    @Override
    public QueryWrapper<ClcQuestionAnswerEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcQuestionAnswerEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date","sort");

        return wrapper;
    }

    @Override
    public List<ClcQuestionAnswerDTO> getListByQuestionId(Long ...questionIds) {
        List<ClcQuestionAnswerEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcQuestionAnswerEntity>()
                        .lambda().in(ClcQuestionAnswerEntity::getQuestionId, Arrays.asList(questionIds))
        );

        return ConvertUtils.sourceToTarget(entities, ClcQuestionAnswerDTO.class);
    }

    @Override
    public List<ClcQuestionAnswerDTO> getByIds(Long... ids) {
        List<ClcQuestionAnswerEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcQuestionAnswerEntity>()
                        .lambda().in(BaseEntity::getId, Arrays.asList(ids))
        );

        return ConvertUtils.sourceToTarget(entities, ClcQuestionAnswerDTO.class);
    }

    @Override
    @Transactional
    public void saveBatch(List<ClcQuestionAnswerDTO> dtos) {
        List<ClcQuestionAnswerEntity> entities = ConvertUtils.sourceToTarget(dtos, ClcQuestionAnswerEntity.class);

        this.insertBatch(entities);
    }

    @Override
    @Transactional
    public void deleteByQuestionIds(Long... questionIds) {
        List<ClcQuestionAnswerEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcQuestionAnswerEntity>()
                        .lambda().in(ClcQuestionAnswerEntity::getQuestionId, Arrays.asList(questionIds))
        );

        Long[] ids = entities.stream().map(BaseEntity::getId).toArray(Long[]::new);
        this.delete(ids);
    }

    @Override
    public Integer getMaxSort() {
        return baseDao.getMaxSort()+1;
    }
}
